//
//  LPLeftHeaderView.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/15.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPLeftHeaderView.h"

@interface LPLeftHeaderView ()



@end
@implementation LPLeftHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        /**  添加修改名字的通知 */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(infoChange:) name:@"LPChangeName" object:nil];
        /**  党员报道修改名字通知  */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(baodaoChange:) name:@"changeUserInfo" object:nil];
        /**  添加修改头像通知  */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifiImage:) name:@"changeHeaderImage" object:nil];
        /**  添加修改头像通知  */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifiPhone) name:@"sendPhoneNumber" object:nil];
        self.backgroundColor = [UIColor themeGrayColor];
        [self addSubview:self.backImageView];
        [self.backImageView addSubview:self.headerImageView];
        [self.backImageView addSubview:self.name];
        [self.backImageView addSubview:self.erweimaLabel];
        
        CGFloat imageHeight;
        if (LP_IsIphoneX) {
            imageHeight = self.height + 44;
        }else{
            imageHeight = self.height;
        }
        self.backImageView.sd_layout
        .topEqualToView(self)
        .leftEqualToView(self)
        .rightEqualToView(self)
        .heightIs(imageHeight);
        
        self.headerImageView.sd_layout
        .centerYEqualToView(self.backImageView)
        .leftSpaceToView(self.backImageView, 15 * [UIScreen zj_screenWScale])
        .heightIs(70 * [UIScreen zj_screenHScale])
        .widthIs(70 * [UIScreen zj_screenHScale]);
        
        self.name.sd_layout
        .centerYEqualToView(self.backImageView)
        .heightIs(20 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self.headerImageView, 7 * [UIScreen zj_screenWScale])
        .widthIs(self.width - 70 * [UIScreen zj_screenHScale] - 25 * [UIScreen zj_screenWScale]);
        

        self.erweimaLabel.sd_layout
        .topSpaceToView(self.backImageView, 20 * [UIScreen zj_screenHScale])
        .heightIs(25 * [UIScreen zj_screenHScale])
        .rightSpaceToView(self.backImageView, 20 * [UIScreen zj_screenWScale])
        .widthIs(25 * [UIScreen zj_screenHScale]);
        
        [self.headerImageView sd_setImageWithURL:CREATEURL(ZJUSERINFORMODEL.ORG_ACCOUNT.PHOTO_URL) placeholderImage:PlaceholderHeaderImage];
        self.name.text = [NSString stringWithFormat:@"%@ %@",ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME,ZJUSERINFORMODEL.ORG_ACCOUNT.MOBILE];
        
    }
    return self;
}
- (void)baodaoChange:(NSNotification *)noti{
    self.name.text = [NSString stringWithFormat:@"%@ %@",ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME,ZJUSERINFORMODEL.ORG_ACCOUNT.MOBILE];
}

- (void)infoChange:(NSNotification *)noti{
    self.name.text = [NSString stringWithFormat:@"%@ %@",ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME,ZJUSERINFORMODEL.ORG_ACCOUNT.MOBILE];
}
- (void)notifiImage:(NSNotification *)noti{
    self.headerImageView.image = noti.userInfo[@"image"];
}

- (void)notifiPhone{
self.name.text = [NSString stringWithFormat:@"%@ %@",ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME,ZJUSERINFORMODEL.ORG_ACCOUNT.MOBILE];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)tapErweima{
    if (self.delegate) {
        [self.delegate LPLeftHeadertap_erweima:YES];
    }

}

- (void)tapHeader{
    if (self.delegate) {
        [self.delegate LPLeftHeadertap_header:YES];
    }
}

- (UIImageView *)backImageView{
    if (!_backImageView) {
        _backImageView = [UIImageView new];
        _backImageView.image = [UIImage imageNamed:@"sidebar_bg"];
        _backImageView.userInteractionEnabled = YES;
        _backImageView.contentMode = UIViewContentModeScaleToFill;
    }
    return _backImageView;
}

- (UIImageView *)erweimaLabel{
    if (!_erweimaLabel) {
        _erweimaLabel = [UIImageView new];
        _erweimaLabel.contentMode = UIViewContentModeCenter;
        _erweimaLabel.image = [UIImage imageNamed:@"erweima_white"];
        _erweimaLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapErweima)];
        [_erweimaLabel addGestureRecognizer:tap];
    }
    return _erweimaLabel;
}

- (UIImageView *)headerImageView{
    if (!_headerImageView) {
        _headerImageView = [UIImageView new];
        _headerImageView.contentMode = UIViewContentModeScaleToFill;
        _headerImageView.layer.cornerRadius = 35 * [UIScreen zj_screenHScale];
        _headerImageView.layer.masksToBounds = YES;
        _headerImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeader)];
        [_headerImageView addGestureRecognizer:tap];
    }
    return _headerImageView;
}

- (UILabel *)name{
    if (!_name) {
        _name = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPBOLDFONT(18);
            label;
        });
    }
    return _name;
}
@end
